﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using B4Deal.Models;
namespace B4Deal.Controllers
{
    public class AdminController : Controller
    {
        //
        // GET: /Admin/
        private static DealManager dealManager = new DealManager();
        private static B4Deal_DataEntities localDB = B4DealDatabase.b4Database;
        private const String adminUsername = "admin";
        private const String adminPass = "admin";
        public ActionResult Index(string user, string pass)
        {
            if (!string.IsNullOrEmpty(user) && !string.IsNullOrEmpty(pass)
                && user.Equals(adminUsername) && pass.Equals(adminPass)
                && Session["admin"] == null)
                Session["admin"] = "logon";
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View(new DashBoard());
            }
            return null;
        }

        public ViewResult DealEdit(int id)
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {        
               return View(dealManager.GetDeal(id));
            }
            return null;
        }

        [HttpPost]
        public ActionResult DealEdit(KHUYENMAI deal)
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                if (!TryUpdateModel(deal))
                {
                    ViewBag.updateError = "Update Failure";
                    return View(deal);
                }

                // ToDo: add persistent to DB.
                dealManager.Update(deal);
                return RedirectToAction("Deals");
            }
            return null;

        }


        public ViewResult DeleteDeal(int id)
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                dealManager.Remove(dealManager.GetDeal(id));
                return View("Deals", dealManager.GetAllDeal());
            //  return View(dealManager.GetDeal(id));
            }
            return null;
        }

        [HttpPost]
        public ViewResult DeleteDeal(KHUYENMAI dealtoDelete)
        {
            if (!TryUpdateModel(dealtoDelete))
            {
                ViewBag.updateError = "Create Failure";
                return View(dealtoDelete);
            }
            //dealManager.Remove(dealtoDelete);
            return View("Deals", dealManager.GetAllDeal());
        }
        [HttpPost]
        public ViewResult CreateDeal(Deal deal)
        {
            if (!TryUpdateModel(deal))
            {
                ViewBag.updateError = "Create Failure";
                return View(deal);
            }

            // ToDo: add persistent to DB.
            dealManager.Create(deal);
            return View("Deals", dealManager.GetAllDeal());
        }
        public ViewResult CreateDeal()
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View(new Deal());
            }
            return null;
        }
    
        public ViewResult DealDetails(int id)
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View(dealManager.GetDeal(id));
            }
            return null;
        }
        public ActionResult Deals1()
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View(dealManager.GetAllDeal());
            }
            return null;
        }
        public ActionResult Deals()
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View(dealManager.GetAllDeal());
            }
            return null;
        }
        public ActionResult Orders()
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View();
            }
            return null;
        }
        public ActionResult Comments()
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View();
            }
            return null;
        }
        public ActionResult Business()
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View();
            }
            return null;
        }
        public ActionResult Members()
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View();
            }
            return null;
        }
        public ActionResult Statistics_Sales()
        {
            if (Session["admin"] != null && Session["admin"].Equals("logon"))
            {
                return View();
            }
            return null;
        }

        public ActionResult Logout()
        {
            Session.Clear();
            return RedirectToAction("Index", "Home");
        }
    }
}
